package com.bgrc.hubu12305.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bgrc.hubu12305.common.utils.UUIDUtils;
import com.bgrc.hubu12305.dao.UserDao;
import com.bgrc.hubu12305.entity.UserEntity;
import com.bgrc.hubu12305.service.UserService;
import com.bgrc.hubu12305.vo.UserRegisterVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 包名:com.bgrc.hubu12305.service.impl
 * 用户:86182
 * 时间:20211210:372021/12/11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public UserEntity findUserByName(String userName) {
        UserEntity userEntity = this.getOne(new QueryWrapper<UserEntity>().eq("user_name", userName));
        return userEntity;
    }

    @Override
    public boolean saveUser(UserRegisterVO userRegisterVO) {
        UserEntity userEntity = new UserEntity();
        String uuid = UUIDUtils.getUUID();
        userEntity.setId(uuid);
        userEntity.setUserName(userRegisterVO.getUserName());
        userEntity.setGender(userRegisterVO.getGender());
        userEntity.setIdentityCard(userRegisterVO.getIdentityCard());
        userEntity.setTel(userRegisterVO.getTel());
        userEntity.setPwd(userRegisterVO.getPwd());
        boolean b = this.save(userEntity);
        /*
            写一个触发器或者存储过程，当用户表增加一天数据时
            在t_role_user_relation增加一条数据，role_id=100，user_id=当前用户id，
        * */
        return b;
    }

    @Override
    public boolean modifyPasswordByName(String pwd) {
        Subject subject = SecurityUtils.getSubject();
        UserEntity userEntity = (UserEntity) subject.getPrincipal();
        String userName = userEntity.getUserName();
        boolean b = userDao.modifyPasswordByName(userName, pwd);
        return b;
    }
}
